layui.use(['jquery','layer','form','table','laydate','upload'],function() {
    var $ = layui.$, layer = layui.layer, form = layui.form, table = layui.table, laydate = layui.laydate,
        upload = layui.upload;

    //开始时间 赵 22/1/21
    laydate.render({
        elem: '#startDate'
    });
    //结束时间 赵 22/1/21
    laydate.render({
        elem: '#expireDate'
    });

    var carouselId = parent.$('#carouselId').val();
    $('input[name=carouselId]').val(carouselId)
    //根据id获取轮播图信息 赵 22/1/21
    $.ajax({
        url: '/advertisement/getRotationCharts'
        ,data: {'carouselId': carouselId}
        ,dataType: 'json'
        ,success:function(data){

            $('input[name=bannerName]').val(data.data[0].bannerName);
            /*$.each(data.data[0].imgType, function (index, item) {
                $('select[name=imgType]').append(new Option(item));
            });*/
            layui.form.render("select");
            $('select[name=imgType]').val(data.data[0].imgType);
            $('select[name=imgType]').val(data.data[0].imgType);
            $('input[name=startDate]').val(data.data[0].startDate);
            $('input[name=expireDate]').val(data.data[0].expireDate);
            if(data.data[0].display == 2){
                $('input[name=display][value="2"]').attr('checked',true);
            }else if(data.data[0].display == 3){
                $('input[name=sex][value="3"]').attr('checked',true);
            }
            $('#imgAddress').attr('src',data.data[0].imgAddress);
            $('input[name=imgAddress]').val(data.data[0].imgAddress);
            $('textarea[name=remarks]').val(data.data[0].remarks);
        }
    });


    //上传广告封面 22/1/21
    var uploadInst = upload.render({
        elem: '#imgAddress'
        ,auto: true
        ,url: ''
        ,field: 'file'
        ,choose:function(obj){
            var files = obj.pushFile();
            obj.preview(function(index,file,result){
                $('#avatarUrl').attr('src',result);
            })
        }
        ,done:function (res) {
            $('input[name=avatarUrl]').val(res.data);
        }
    });

    //编辑完成提交
    form.on('submit(sub)',function(data){
        var rotationChart = data.field;
        console.log(rotationChart);
        $.ajax({
            url: '/advertisement/editRotationChart'
            ,data: rotationChart
            ,dataType: 'json'
            ,success:function(data){
                var layer = parent.layer;
                layer.close(layer.index);
                layer.msg('修改成功');
            }
        });
    });

    //取消 赵 22/1/21
    $('#cancel').click(function(){
        var layer = parent.layer;
        layer.close(layer.index);
    });



})